package me.shurufa.activities;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import butterknife.Bind;
import me.shurufa.R;
import me.shurufa.fragments.DigestAddNoteFragment;
import me.shurufa.model.AddDigestBean;
import me.shurufa.utils.Constants;
import me.shurufa.utils.Utils;

/**
 * 作者 : andy
 * 日期 : 16/3/14 14:19
 * 邮箱 : andyxialm@gmail.com
 * 描述 : 书摘添加笔记界面
 */
public class BookDigiestAddNoteActivity extends BaseActivity implements View.OnClickListener {


    @Bind(R.id.left_text)
    TextView leftText;

    @Bind(R.id.right_image)
    ImageView rightImage;

    @Bind(R.id.right_text)
    TextView rightText;

    @Bind(R.id.toolbar_title)
    TextView toolbar_title;

    private AddDigestBean mAddDigestBean;
    private String mAbsolutePath;
    private DigestAddNoteFragment mDigestAddNoteFragment;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        activity_layout_res = R.layout.activity_digest_add_note;
        super.onCreate(savedInstanceState);
        parseArguments();
        initUI();
    }

    /**
     * 传参
     * @param in
     * @param addDigestBean
     */
    public static void initArguments(Intent in, String absolutePath, AddDigestBean addDigestBean) {
        in.putExtra(Constants.EXTRA_CAMERA_PHOTO, absolutePath);
        in.putExtra(Constants.ARG_ADD_DIGEST, addDigestBean);
    }

    /**
     * 解析参数
     */
    private void parseArguments() {
        this.mAddDigestBean = (AddDigestBean) getIntent().getSerializableExtra(Constants.ARG_ADD_DIGEST);
        this.mAbsolutePath = getIntent().getStringExtra(Constants.EXTRA_CAMERA_PHOTO);
    }


    private void initUI() {
        titleTextView.setText(R.string.add_note);
        rightText.setText(getString(R.string.next));
        rightText.setVisibility(View.VISIBLE);
        rightText.setOnClickListener(this);

        initFragment();
    }

    private void initFragment() {
        FragmentManager fm = getSupportFragmentManager();
        FragmentTransaction ft = fm.beginTransaction();
        mDigestAddNoteFragment = DigestAddNoteFragment.newInstance(mAbsolutePath, mAddDigestBean);
        ft.replace(R.id.fragment_container, mDigestAddNoteFragment);
        ft.commit();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.right_text:
                if (checkData()) {
                    onNext();
                }
                break;
        }
    }

    /**
     * 右上角点击下一步
     */
    private void onNext() {
        Intent in = new Intent(this, SelectCatalogActivity.class);
        SelectCatalogActivity.initArguments(in, mAddDigestBean);
        startActivity(in);
    }

    @Override
    public void onEventMainThread(Intent intent) {
        super.onEventMainThread(intent);
        if (Constants.EVENT_COMPLETE_ADD_DIGEST.equals(intent.getAction())) {
            finish();
        }
    }

    /**
     * 校验数据
     * @return
     */
    private boolean checkData() {
        mAddDigestBean = mDigestAddNoteFragment.getAddDigestBean();
        if (null == mAddDigestBean) {
            Utils.showToast(getString(R.string.write_book_digest));
            return false;
        }

        if (TextUtils.isEmpty(mAddDigestBean.content) && Constants.DIGEST_TYPE_TEXT == mAddDigestBean.type) {
            Utils.showToast(getString(R.string.write_book_digest));
            return false;
        }

        if ("0".equals(mAddDigestBean.excerptedBookId) || TextUtils.isEmpty(mAddDigestBean.excerptedBookTitle)) {
            Utils.showToast(getString(R.string.select_book_digest));
            return false;
        }

        // 若是文字书摘 则限制长度为10-500
        if (0 == mAddDigestBean.type) {
            if (!TextUtils.isEmpty(mAddDigestBean.content)) {
                if (mAddDigestBean.content.length() < 10) {
                    Utils.showToast("书摘内容不可少于10字");
                    return false;
                }
                if (mAddDigestBean.content.length() > 500) {
                    Utils.showToast("书摘内容不可超过500字");
                    return false;
                }
            } else {
                Utils.showToast("书摘内容不可少于10字");
                return false;
            }
        }


        // 笔记
        if (!TextUtils.isEmpty(mAddDigestBean.note)) {
            if (mAddDigestBean.note.length() < 10) {
                Utils.showToast("笔记内容不可少于10字");
                return false;
            }

            if (mAddDigestBean.note.length() > 3000) {
                Utils.showToast("笔记内容不可超过3000字");
                return false;
            }
        }

        return true;
    }

}
